/*jshint -W069 */
import axios, { AxiosResponse,AxiosRequestConfig } from 'axios'
// dtos

<% definitions.forEach( element => {%>
type <%= element.name.replace("«","").replace("»","") %> = {
  <% element.tsType.properties.forEach( propertity => { %><%= propertity.name %><% if ( propertity.optional ) { %>? <% } %>: <% if ( propertity.isRef ) { %> <%= propertity.target %> <% } %><% if ( propertity.isAtomic ) { %> <%= propertity.tsType %> <% } %> <% if ( propertity.isArray ) { %> Array <<%=propertity.elementType.target %>> | <%=propertity.elementType.target %><% } %>
  <% } ) %>  
}
<%} ) %>

/**
 * <%=description%>
 * @class <%=className%>
 * @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
 * @param {string} [domainOrOptions.domain] - The project domain
 * @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
 */
export default class <%=className %> {

  $defaultDomain:string

  /**
   *  <%=className%>构造函数
   * @param domain API域名
   */
  constructor( domain?: string ){
    this.$defaultDomain = domain || '<%=defaultDomain%>';
  }

  <% methods.forEach( method => {%>  
    /**
    * <%=method.summary%>
    * @method
    * @name <%=className %>#<%=method.summary%>
    <% method.parameters.forEach( parameter => { %>* @param <%=parameter.type %> <%=parameter.camelCaseName %> - <%=parameter.description %> <%} ) %>
    * @param $domain API域名,没有指定则使用构造函数指定的
    */
    <%=method.methodName %> ( parameters : {
      <% method.parameters.forEach( parameter => {
        let  paramType = parameter.tsType.target || parameter.tsType.tsType;
        if(paramType === "array"){
          paramType = "Array<"+parameter.tsType.elementType.target+">"
        } %>'<%=parameter.camelCaseName %>'  <%=parameter.cardinality %>: <%- paramType  %>,
      <%} ) %>$queryParameters ?: any,
      $domain ?: string
    }): Promise < AxiosResponse > { 

      let body: any
      let config: AxiosRequestConfig = {
        baseURL: parameters.$domain || this.$defaultDomain,
        url: '<%=method.path %>',
        method: '<%=method.method %>'
      }
    
      config.headers = {}
      <% if ( method.headers ) { %><% method.headers.forEach( header => {%>
      config.headers['<%=header.name%>'] = <%-header.value%>;<% }) %><% } %>
      <% method.parameters.forEach( parameter => { %>
      <% if (parameter.isHeaderParameter ) { %>config.headers['<%=parameter.camelCaseName%>'] = parameters.<%=parameter.camelCaseName%>;<% } %><% if ( parameter.isPathParameter ) { %>config.url = config.url.replace('{<%=parameter.camelCaseName%>}',parameters.<%=parameter.camelCaseName%>+'');<% } %><% if ( parameter.isBodyParameter ) {%>body = parameters.<%=parameter.camelCaseName %>;
      config.data = body;<% } %><% } ) %>
      
      return axios.request( config );
    }
  <%} ) %>
    
}